package Utils.web;

import Utils.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * URL工具类
 *
 * @author d11yu
 */
public class UrlUtils {
    /**
     * 从url下载文件。
     *
     * @param urlStr url地址
     * @param path 文件保存路径
     */
    public static void downloadFile(String urlStr, String path) throws IOException {
        URL url = new URL(urlStr);
        downloadFile(url, path);
    }

    /**
     * 从url下载文件。
     *
     * @param url  url地址
     * @param path 文件保存路径
     */
    public static void downloadFile(URL url, String path) throws IOException {
        HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
        urlCon.connect();
        try (InputStream is = urlCon.getInputStream()) {
            IOUtils.inputStream2File(is, path);
        } finally {
            urlCon.disconnect();
        }
    }
}
